package task.dao;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class ObjectDb {
	private XStream xstream = new XStream(new DomDriver());
	private Map<String, Object> map;
	private File file;
	public ObjectDb(String fileName) {
		file = new File(fileName);
		try {
			FileReader is = new FileReader(file);
			map = (Map<String, Object>) xstream.fromXML(is);
			is.close();
		} catch (Exception e) {
			map = new HashMap<String, Object>();
			e.printStackTrace();
		}
	
	}
	public Object get(String key) {
		return map.get(key);
	}
	public Object put(String key, Object value) {
		return map.put(key, value);
	}
	public void save() {
		try {
			FileWriter os = new FileWriter(file);
			xstream.toXML(map, os);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Set<String> keySet() {
		return map.keySet();
	}
	public Object remove(String key) {
		return map.remove(key);
	}
	public void backup(String fileName) {
		File outFile = new File(fileName);
		try {
			FileWriter os = new FileWriter(outFile);
			xstream.toXML(map, os);
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
